package user.controllers;

import helpers.LanguageHelper;

import java.io.IOException;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import user.forms.UserForm;
import user.models.UserModel;

import entities.ApplicationConfig;
import entities.User;

public class ChangeAvatarServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		ApplicationConfig appConfig = (ApplicationConfig) request.getAttribute("sysConfig");
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		String status = (String) request.getParameter("status");
		if (status != null) {
			HttpSession session = request.getSession();
			if (session.getAttribute("check") != null) {
				check = (TreeMap) session.getAttribute("check");
				session.removeAttribute("check");
			}
			
			if (session.getAttribute("success") != null) {
				request.setAttribute("success", session.getAttribute("success"));
				session.removeAttribute("success");
			}
		}
		
		User user = null;
		if (request.getUserPrincipal() != null) {
			UserModel userModel = UserModel.getInstance();
			user = userModel.getByUsername(appConfig.getApplicationId(), request.getUserPrincipal().getName());
		}
		
		if (user == null) {
			response.sendRedirect("/msg/require-login");
			return;
		}
		
		request.setAttribute("user", user);
		request.setAttribute("check", check);
		request.setAttribute("form", "edit");
		try {
			request.getRequestDispatcher("/user/pages/change_avatar.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		ApplicationConfig appConfig = (ApplicationConfig) request.getAttribute("sysConfig");
		
		String userid = (String) request.getParameter("userid");
		
		UserModel userModel = UserModel.getInstance();
		User user = userModel.getUser(appConfig.getApplicationId(), userid);
		
		if (user == null) {
			response.sendRedirect("/msg/require-login");
			return;
		}
		
		TreeMap forms = (TreeMap) UserForm.checkValidAvatar(request, user);
		user = (User) forms.get("user");
		TreeMap check = (TreeMap) forms.get("checkValid");
		//check = CaptchaForm.checkValid(request, check);
		
		if (check.size() == 0) {
			userModel.insert(appConfig.getApplicationId(), user);
			
			HttpSession session = request.getSession();
			session.setAttribute("user_logged_in", user);
			session.setAttribute("success", "Change Avatar Success");
			
			response.sendRedirect("/profile/change-avatar?status=success");
			return;
		} else {
			HttpSession session = request.getSession();
			session.setAttribute("check", check);
			
			response.sendRedirect("/profile/change-avatar?status=unsuccess");
			return;
		}
		
		/*request.setAttribute("user", user);
		request.setAttribute("check", check);
		request.setAttribute("form", "edit");
		try {
			request.getRequestDispatcher("/pages/change_avatar.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}*/
	}
}
